package com.hezuti.novel.parser;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Parser;

import com.hezuti.novel.domain.Book;
import com.hezuti.novel.domain.Chapter;
import com.hezuti.novel.domain.Volume;
import com.hezuti.util.HttpDownload;

/**
 * @author Administrator
 */
public abstract class NovelParser {

	protected  Parser  parser;
	
	protected String content;
	
	protected Book book=null;
	
	protected abstract String getEncode();

	protected abstract String getDomain();

	protected void excute(List<String> bookUrlList,boolean useparesr) {
		for (String url : bookUrlList) {
			System.out.println("start do: "+url);
			book=new Book();
			book.setUri(url);
			book.setFrom(getDomain());
			try{
				content=HttpDownload.getContent(url, getEncode());
			if(content==null){
				continue;
			}
			}catch(Exception e){
				e.printStackTrace();
			}
			if(useparesr){
				try{
				parser=new Parser(content);}catch(Exception e){
					e.printStackTrace();
				}
			}
			getBookInfo();
		}

	}
	protected abstract void getBookInfo();
	/**
	 * 
	 * @param content
	 * @return
	 */
	protected abstract void getChapterUrlListByVol();

	/**
	 * @param chapterUrlList
	 * @return
	 */
	protected void getChapterList(List<Volume> volumes) {
		if(volumes==null){
			return ;
		}
		for (Volume volume : volumes) {
			List<Chapter> chapters = volume.getChapters();
			for (Chapter chapter : chapters) {
				chapter = buildChapter(HttpDownload.getContent(
						chapter.getUri(), getEncode()));
				if (chapter == null) {
					chapter = new Chapter();
				}
			}
		}
	}

	/**
	 * @param content
	 * @return
	 */
	protected abstract Chapter buildChapter(String content);

}
